package com.ymatou.iapi.messageroute.testcase;

import java.util.HashMap;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.messageroute.parameter.BizTypeEnum;
import com.ymatou.iapi.messageroute.parameter.MsgTask;
import com.ymatou.iapi.messageroute.parameter.SendMsgBean;
import com.ymatou.iapi.messageroute.service.SendMsgCall;
import com.ymatou.iapi.messageroute.service.SendTradingMsgService;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderService;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.utils.TestDataManager;

/**
 *  消息路由接受MQ消息接口
 */
@TestSuite
public class Ts_SendMsg {

	private static SendMsgBean sendMsgBean;
	private static SendMsgCall sendMsgCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("消息路由接受MQ消息接口");

	}

	@Before
	public void caseUp() {
		sendMsgBean = new SendMsgBean();
		sendMsgCall = new SendMsgCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult("");
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SendMsg_001() {
		Logger.start(true, "SEND_COUPON 发放商家优惠卷");
		try {

			PlaceOrderResp placeOrderResp = PlaceOrderService
					.placeOrderWithOutCoupon();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			int userId = placeOrderResp.getUserId();

			sendMsgBean.setAppId("coupon");
			sendMsgBean.setBizType("SendCoupon");
			sendMsgBean.setBizKey("couponAutotest"
					+ TestDataManager.getRandomNum(6));

			Map<String, String> map = new HashMap<String, String>();

			map.put("CouponCount", "1");
			map.put("UserId", String.valueOf(userId));
			map.put("CouponAmount", "4.00");
			map.put("OrderAmount", "199.00");
			map.put("CouponName", "春暖花开季");
			map.put("SellerId", "3383");
			map.put("ValidStart", "2017-04-01T14:41:56");
			map.put("ValidEnd", "2017-04-30T23:59:59");
			map.put("LimitProductType", "1");
			map.put("LimitUserType", "0");
			map.put("LimitText", "autotest");

			sendMsgBean.setBizParam(map);

			sendMsgCall.setData(sendMsgBean);
			sendMsgCall.callService();

			Logger.verifyEquals("ok", sendMsgCall.getReturnData(), "验证接口返回");

			// 验证推送消息

			MsgTask actualMsgTask = SendTradingMsgService.getMsgTaskByOrderId(
					BizTypeEnum.SEND_COUPON, sendMsgBean.getBizKey());

			Logger.verifyEquals(1, actualMsgTask.getMsgItems().size(),
					"验证优惠券发送渠道为1");

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SendMsg_002() {
		Logger.start(true, "seller UnpublishedProduct 手机验证仍未提交审核材料的第3天（72小时）");
		try {

			int sellerId = 20238699;

			sendMsgBean.setAppId("sellergrowth");
			sendMsgBean.setBizType("UnpublishedProduct");
			sendMsgBean.setBizKey(String.valueOf(sellerId));

			Map<String, String> map = new HashMap<String, String>();

			map.put("AuditFailReason", "1");
			map.put("SellerName", "wesper");
			map.put("UserId", String.valueOf(sellerId));
			map.put("SellerEmail", "sunsheng@ymatou.com");

			sendMsgBean.setBizParam(map);

			sendMsgCall.setData(sendMsgBean);
			sendMsgCall.callService();

			Logger.verifyEquals("ok", sendMsgCall.getReturnData(), "验证接口返回");

			// 验证推送消息

			MsgTask actualMsgTask = SendTradingMsgService.getMsgTaskByOrderId(
					BizTypeEnum.UnpublishedProduct, sendMsgBean.getBizKey());

			Logger.verifyEquals(4, actualMsgTask.getMsgItems().size(),
					"验证优惠券发送渠道为4");

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SendMsg_003() {
		Logger.start(true, "seller UnSubmitMaterial 手机验证仍未提交审核材料的第3天（72小时）");
		try {

			int sellerId = 20238699;

			sendMsgBean.setAppId("sellergrowth");
			sendMsgBean.setBizType("UnSubmitMaterial");
			sendMsgBean.setBizKey(String.valueOf(sellerId));

			Map<String, String> map = new HashMap<String, String>();

			map.put("AuditFailReason", "1");
			map.put("SellerName", "wesper");
			map.put("UserId", String.valueOf(sellerId));
			map.put("SellerEmail", "sunsheng@ymatou.com");

			sendMsgBean.setBizParam(map);

			sendMsgCall.setData(sendMsgBean);
			sendMsgCall.callService();

			Logger.verifyEquals("ok", sendMsgCall.getReturnData(), "验证接口返回");

			// 验证推送消息

			MsgTask actualMsgTask = SendTradingMsgService.getMsgTaskByOrderId(
					BizTypeEnum.UnSubmitMaterial, sendMsgBean.getBizKey());

			Logger.verifyEquals(4, actualMsgTask.getMsgItems().size(),
					"验证优惠券发送渠道为4");

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SendMsg_004() {
		Logger.start(true, "seller AuditPassed 买手材料审核通过");
		try {

			int sellerId = 20238699;

			sendMsgBean.setAppId("sellergrowth");
			sendMsgBean.setBizType("AuditPassed");
			sendMsgBean.setBizKey(String.valueOf(sellerId));

			Map<String, String> map = new HashMap<String, String>();

			map.put("AuditFailReason", "1");
			map.put("SellerName", "wesper");
			map.put("UserId", String.valueOf(sellerId));
			map.put("SellerEmail", "sunsheng@ymatou.com");

			sendMsgBean.setBizParam(map);

			sendMsgCall.setData(sendMsgBean);
			sendMsgCall.callService();

			Logger.verifyEquals("ok", sendMsgCall.getReturnData(), "验证接口返回");

			// 验证推送消息

			MsgTask actualMsgTask = SendTradingMsgService.getMsgTaskByOrderId(
					BizTypeEnum.AuditPassed, sendMsgBean.getBizKey());

			Logger.verifyEquals(2, actualMsgTask.getMsgItems().size(),
					"验证优惠券发送渠道为2");

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SendMsg_005() {
		Logger.start(true, "seller AuditFailure 买手材料审核失败");
		try {

			int sellerId = 20238699;

			sendMsgBean.setAppId("sellergrowth");
			sendMsgBean.setBizType("AuditFailure");
			sendMsgBean.setBizKey(String.valueOf(sellerId));

			Map<String, String> map = new HashMap<String, String>();

			map.put("AuditFailReason", "1");
			map.put("SellerName", "wesper");
			map.put("UserId", String.valueOf(sellerId));
			map.put("SellerEmail", "sunsheng@ymatou.com");

			sendMsgBean.setBizParam(map);

			sendMsgCall.setData(sendMsgBean);
			sendMsgCall.callService();

			Logger.verifyEquals("ok", sendMsgCall.getReturnData(), "验证接口返回");

			// 验证推送消息

			MsgTask actualMsgTask = SendTradingMsgService.getMsgTaskByOrderId(
					BizTypeEnum.AuditFailure, sendMsgBean.getBizKey());

			Logger.verifyEquals(2, actualMsgTask.getMsgItems().size(),
					"验证优惠券发送渠道为2");

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SendMsg_006() {
		Logger.start(true, "物流推送通知");
		try {

			int orderId = PlaceOrderService.placeOrderWithOutCoupon()
					.getOrderList().get(0).getOrderId();

			int status = 5;

			sendMsgBean.setAppId("logistics");
			sendMsgBean.setBizType("LogisticsNotify");
			sendMsgBean.setBizKey(orderId + "_" + status);

			Map map = new HashMap();

			map.put("OrderId", orderId);
			map.put("BillCode", TestDataManager.getRandomNum(6));
			map.put("NodeStatus", status);

			sendMsgBean.setBizParam(map);

			sendMsgCall.setData(sendMsgBean);
			sendMsgCall.callService();

			Logger.verifyEquals("ok", sendMsgCall.getReturnData(), "验证接口返回");

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
